﻿<?php
require('configs/include.php');

class c_index extends super_controller {

    public function login() {

        if (is_empty($this->post->username) && is_empty($this->post->password)) {
            throw_exception("Diligencie todos los campos");
        }

        $cod['usuario']['username'] = $this->post->username;
        $cod['usuario']['password'] = $this->post->password;
        $options['usuario']['lvl2'] = "one_login";
        $options['programador']['lvl2'] = "all";
        $components['usuario']['programador'] = array("u_p");

        $this->orm->connect();
        $this->orm->read_data(array("usuario", "programador"), $options, $cod);
        $usuario = $this->orm->get_objects("usuario", $components);
        $this->orm->close();

        if (isset($usuario)) {
            if (count($usuario[0]->components['u_p']) == 0) {
                $_SESSION['usuario']['cedula'] = $usuario[0]->get('cedula');
                $_SESSION['usuario']['nombre'] = $usuario[0]->get('nombre');
                $_SESSION['usuario']['username'] = $usuario[0]->get('username');
                 $_SESSION['usuario']['foto'] = $usuario[0]->get('foto');
                $this->session = $_SESSION;
            } else {
                $_SESSION['usuario']['cedula'] = $usuario[0]->get('cedula');
                $_SESSION['usuario']['nombre'] = $usuario[0]->get('nombre');
                $_SESSION['usuario']['username'] = $usuario[0]->get('username');
                $_SESSION['usuario']['foto'] = $usuario[0]->get('foto');
                $_SESSION['usuario']['reputacion'] = $usuario[0]->components['u_p'][0]->get('reputacion');
                $_SESSION['usuario']['tipo'] = $usuario[0]->components['u_p'][0]->get('tipo');
                $this->session = $_SESSION;
            }
        } else {
            throw_exception("Datos erróneos");
        }
    }

    public function logout() {
        session_destroy();
        unset($this->session);

        $this->type_warning = "success";
        $this->msg_warning = "Se desconecto correctamente";
        $this->img_warning = "correct.png";
        $this->engine->assign('img_warning', $this->img_warning);
        $this->engine->assign('type_warning', $this->type_warning);
        $this->engine->assign('msg_warning', $this->msg_warning);
        $this->temp_aux = 'message.tpl';
    }

    public function display() {
        $this->engine->assign('title', $this->gvar['n_index']);
        $this->engine->assign('ubicationPath', $this->gvar['n_index']);
        $this->engine->display('header.tpl');
        $this->engine->display('index.tpl');
        $this->engine->display($this->temp_aux);
        $this->engine->display('footer.tpl');
    }

    public function run() {
        try {
            if (isset($this->get->option)) {
                $this->{$this->get->option}();
            }
        } catch (Exception $e) {
            $this->error = 1;
            $this->msg_warning = $e->getMessage();
            $this->img_warning = "incorrect.png";
            $this->engine->assign('type_warning', $this->type_warning);
            $this->engine->assign('msg_warning', $this->msg_warning);
            $this->engine->assign('img_warning', $this->img_warning);
            $this->temp_aux = 'message.tpl';
        }
        $this->display();
    }

}

$call = new c_index();
$call->run();
?>